package com.software.levelupandroid.home.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.ui.ToastUtil;
import com.software.levelupandroid.systemTask.model.SystemShowViewModel;
import com.software.levelupandroid.systemTask.presenter.TaskPresenter;
import com.software.levelupandroid.taskIssue.view.activity.TaskIssueActivity;

import java.util.ArrayList;
import java.util.List;

import cn.ljp.swipemenu.SwipeMenuLayout;


public class DeleteItemAdapter extends RecyclerView.Adapter<DeleteItemAdapter.ViewHolder> {

    private static final String TAG = "ItemAdapter";
    private TaskPresenter mTaskPresenter;
    private SystemShowViewModel mSystemShowViewModel;
    private Context mContext;
    private List<SystemTask> mShowItems = new ArrayList<>();

    public DeleteItemAdapter(Context context, List<SystemTask> items, TaskPresenter taskPresenter, SystemShowViewModel systemShowViewModel) {
        mContext = context;
        mShowItems = items;
        mTaskPresenter = taskPresenter;
        mSystemShowViewModel = systemShowViewModel;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_system_task_item, viewGroup, false);
        return new ViewHolder(view);
    }
    public void updateTaskList(List<SystemTask> tasks){
        mShowItems.clear();
        mShowItems.addAll(tasks);
        notifyDataSetChanged();
    }


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, @SuppressLint("RecyclerView") final int i) {
        final String item = mShowItems.get(i).getTitle();
        viewHolder.mSwipe.setEnableLeftMenu(false);
        String text = item;

        viewHolder.mSwipe.setOpenChoke(true);

        viewHolder.mSwipe.setClickMenuAndClose(true);

        viewHolder.mTv1.setText(text);


        viewHolder.mTime.setText(mShowItems.get(i).getTime());

        if (mShowItems.get(i).getStatus() == 0) {
            viewHolder.mStatus.setText("任务状态:未完成");
        } else if(mShowItems.get(i).getStatus() == 1){
            viewHolder.mStatus.setText("任务状态:已完成");
            viewHolder.mTv4.setVisibility(View.GONE);
            viewHolder.mLayout.setBackgroundResource(R.color.light_blue);
        }

        viewHolder.mSwipe.setOnClickListener(new View.OnClickListener() {
//        viewHolder.mLl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewHolder.mSwipe.expandMenuAnim();

            }
        });
        //删除
        viewHolder.mTv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // ToastUtil.showToast(mContext, "点击了菜单->删除");
                Log.d(TAG, "onClick: 点击了菜单->删除");
                //用这个 主要是解决了之前有个删除后刷新，其他条目菜单也会做个菜单动画bug
                notifyDataSetChanged();

                // 删除task
                SystemTask task = new SystemTask();
                task.setSystemTaskId(mShowItems.get(i).getSystemTaskId());
                task.setTime(mShowItems.get(i).getTime());
                task.setTitle(mShowItems.get(i).getTitle());
                task.setType(mShowItems.get(i).getType());
                task.setContent(mShowItems.get(i).getContent());
                task.setStatus(mShowItems.get(i).getStatus());
                mTaskPresenter.deleteTask(task, v.getContext());
                mShowItems.remove(i);
            }
        });

        //完成
        viewHolder.mTv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                viewHolder.mTv4.setVisibility(View.GONE);

                SystemTask task = new SystemTask();
                task.setSystemTaskId(mShowItems.get(i).getSystemTaskId());
                task.setTime(mShowItems.get(i).getTime());
                task.setTitle(mShowItems.get(i).getTitle());
                task.setType(mShowItems.get(i).getType());
                task.setContent(mShowItems.get(i).getContent());
                task.setStatus(1);

                //更改为完成状态
                mSystemShowViewModel.isFinishSystemTask(task, v.getContext());
                viewHolder.mStatus.setText("任务状态:已完成");
                // 更换背景颜色
                viewHolder.mLayout.setBackgroundResource(R.color.light_blue);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mShowItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final View mLl_item;
        private final TextView mTv1;
        private final TextView  mStatus,mTime;
        private final View mTv3, mTv4;
        private final SwipeMenuLayout mSwipe;
        private View mLayout;

        public ViewHolder(@NonNull View view) {
            super(view);
            mLl_item = view.findViewById(R.id.ll_item);
            mTv1 = view.findViewById(R.id.stv_content);
            mTv3 = view.findViewById(R.id.tv_menu2);
            mTv4 = view.findViewById(R.id.tv_menu3);
            mSwipe = view.findViewById(R.id.swipe_menu_layout);
            mStatus = view.findViewById(R.id.task_status);
            mTime = view.findViewById(R.id.tv_timeEnd);
            mLayout = view.findViewById(R.id.background);
        }
    }
}

